//
//  POPTool.m
//  POPTest
//
//  Created by 航汇聚科技 on 2017/8/8.
//  Copyright © 2017年 Yi Zhou. All rights reserved.
//

#import "POPTool.h"

@interface POPTool ()
{
    UIView *_currentView;
}
@end

@implementation POPTool

+ (instancetype)sharedPopTool {
    static POPTool *instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [POPTool new];
    });
    return instance;
}


- (void)popView:(UIView *)view animated:(BOOL)animated {
    //保存当前弹出的视图
    _currentView = view;
    
    CGFloat halfScreenWidth = [UIScreen mainScreen].bounds.size.width / 2;
    CGFloat halfScreenHeight = [UIScreen mainScreen].bounds.size.height / 2;
    
    CGPoint screenCenter = CGPointMake(halfScreenWidth, halfScreenHeight);
    
    view.center = screenCenter;
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:view];
    
    if (animated) {
        //第一步：将view的宽高缩至无限小
        view.transform = CGAffineTransformScale(CGAffineTransformIdentity, CGFLOAT_MIN, CGFLOAT_MIN);
        
        [UIView animateWithDuration:0.3 animations:^{
            //第二步：以动画的形式将view慢慢放至原始大小的1.2倍
            view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.2 animations:^{
                //第三步：以动画的形式将view恢复至原始大小
                view.transform = CGAffineTransformIdentity;
            }];
        }];
    }
}


- (void)closeAnimated:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:0.2 animations:^{
            // 第一步： 以动画的形式将view慢慢放大至原始大小的1.2倍
            _currentView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                // 第二步： 以动画的形式将view缩小至原来的1/1000分之1倍
                _currentView.transform = CGAffineTransformScale( CGAffineTransformIdentity, 0.001, 0.001);
            } completion:^(BOOL finished) {
                // 第三步： 移除
                [_currentView removeFromSuperview];
            }];
        }];
    } else {
        [_currentView removeFromSuperview];
    }
    
}
@end
